﻿#include "topo_coedge.h"
#include "topo_edge.h"

CAGD::TopoCoedge::TopoCoedge()
    : m_twin(nullptr), m_next(nullptr), m_prev(nullptr), m_loop(nullptr), m_wire(nullptr), m_edge(nullptr),
      m_dir(TopoDir::FORWARD)
{
}

CAGD::TopoCoedge::~TopoCoedge()
{
    // 移除 twin
    if (m_twin != nullptr)
    {
        m_twin->Twin() = nullptr;
        m_twin->Edge() = nullptr;
        delete m_twin;
    }

    // 移除 edge
    if (m_edge != nullptr)
    {
        m_edge->Coedge() = nullptr;
        delete m_edge;
    }

    // 断开连接
    if (m_next != nullptr)
        m_next->Prev() = nullptr;
    if (m_prev != nullptr)
        m_prev->Next() = nullptr;
}

Handle(CAGD::Geom2dCurve) CAGD::TopoCoedge::Geom() const
{
    return m_curve;
}

CAGD::TopoCoedge *&CAGD::TopoCoedge::Twin()
{
    return m_twin;
}

CAGD::TopoCoedge *&CAGD::TopoCoedge::Prev()
{
    return m_prev;
}

CAGD::TopoCoedge *&CAGD::TopoCoedge::Next()
{
    return m_next;
}

CAGD::TopoLoop *&CAGD::TopoCoedge::Loop()
{
    return m_loop;
}

CAGD::TopoWire *&CAGD::TopoCoedge::Wire()
{
    return m_wire;
}

CAGD::TopoEdge *&CAGD::TopoCoedge::Edge()
{
    return m_edge;
}

CAGD::TopoDir CAGD::TopoCoedge::Dir() const
{
    return m_dir;
}

bool CAGD::TopoCoedge::Boundary() const
{
    // 如果此半边孤立，则一定在边界上
    return m_prev == nullptr;
}

CAGD::TopoType CAGD::TopoCoedge::Type() const
{
    return TopoType::COEDGE;
}
